package com.write.program.util;

import java.util.concurrent.*;

public class AsyncUtil {

	private static ExecutorService executorService = Executors.newFixedThreadPool(15);
	private static ScheduledThreadPoolExecutor scheduledThreadPool = new ScheduledThreadPoolExecutor(30);

    /**
	 * 添加异步任务
	 * @param task
	 */
	public static void addTask(final Runnable task){
		executorService.submit(task);
	}

	/**
	 * 添加延迟任务,指定延迟时间
	 * @param task
	 * @param delay
	 * @param timeUnit
	 */
	public static void addDelayTask(Runnable task, long delay, TimeUnit timeUnit) {
		scheduledThreadPool.schedule(task, delay, timeUnit);
	}

	/**
	 * 定时任务
	 * @param task
	 * @param initialDelay 第一次延时执行时间
	 * @param period 执行频率
	 * @param unit 时间单位
	 */
	public static void scheduleAtFixedRate(final Runnable task, long initialDelay, long period, TimeUnit unit){
		scheduledThreadPool.scheduleAtFixedRate(task, initialDelay, period, unit);
	}

	/**
	 * 定时任务
	 * @param task
	 * @param initialDelay 第一次延时执行时间
	 * @param delay 两次执行时间间隔
	 * @param unit 时间单位
	 */
	public static void scheduleWithFixedDelay(final Runnable task, long initialDelay, long delay, TimeUnit unit){
		scheduledThreadPool.scheduleWithFixedDelay(task, initialDelay, delay, unit);
	}
}
